//
//  BottomCommentView.swift
//  maosheng
//
//  Created by lxl on 2019/8/20.
//

import UIKit

class BottomCommentView: UIView {
    let commentField = UITextField()
    var blogId : String?
    var collectBtn : UIButton?
    var likeBtn : UIButton?
    var refreshCommentInfo : ClickBtn?
    override init(frame: CGRect) {
        super.init(frame: frame)
        addTheBottomView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func addTheBottomView()  {
        let lineView = UIView()
        lineView.backgroundColor = RGBColor(r: 244, g: 244, b: 244)
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(0)
            make.height.equalTo(0.5)
        
            
            
            
        }
        
        
        let arr_text = ["收藏","点赞",]
        let arr_img = ["detail_shoucang","detail_dianzan"]
        var tmpBtn : UIButton?
        for i in 0..<arr_text.count{
            let btn = UIButton(type: .custom)
            btn.tag = 201 + i
            if i == 0{
                collectBtn = btn
            }else{
                likeBtn = btn

            }
            btn.setTitleColor(KYP_666666_COLOR, for: .normal)
            btn.setTitle(arr_text[i], for: .normal)
            btn.setImage(UIImage(named: "\(arr_img[i])1"), for: .normal)
            btn.setImage(UIImage(named: arr_img[i]), for: .selected)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 11)
            btn.addTarget(self, action: #selector(commentOrLikeClick(btn:)), for: .touchUpInside)
            self.addSubview(btn)
            btn.snp.makeConstraints { (make) in
                make.top.equalTo(8)
                make.bottom.equalTo(0)
                if tmpBtn != nil{
                    make.right.equalTo(tmpBtn!.snp.left).offset(-20)
                }else{
                    make.right.equalTo(-15)
                }
                make.width.equalTo(40)

            }
            self.layoutIfNeeded()
            changeTheBtnImgAndTitle(button: btn)
            tmpBtn = btn
        }
        
        commentField.font = UIFont.systemFont(ofSize: 12)
        commentField.placeholder = "说点什么吧~"
        commentField.backgroundColor = RGBColor(r: 244, g: 244, b: 244)
        commentField.layer.cornerRadius = 20;
        commentField.layer.masksToBounds = true
        commentField.delegate = self
        commentField.returnKeyType = .done
        self.addSubview(commentField)
        commentField.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(5)
            make.bottom.equalTo(-5)
            make.right.equalTo(tmpBtn!.snp.left).offset(-30)
        }
        
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
        commentField.leftView = leftView
        commentField.leftViewMode = .always
    }
    
    func setTheCollectOrLikeState(model : HomeDetialModel)  {
        if let isFollow = model.isFollower{
            collectBtn?.isSelected = isFollow
        }
        if let isAttitude = model.isAttitude{
            likeBtn?.isSelected = isAttitude
        }

    }
    @objc func commentOrLikeClick(btn:UIButton){
        var str_url = ""
        var params = [String : Any]()
        params["moduleTypeId"] = 100301
        params["objectId"] = self.blogId

        if btn.tag == 201 {//收藏
            str_url = btn.isSelected ? "api/services/app/Favorite/DeleteFavoriteAsync" : "api/services/app/Favorite/CreateFavoriteAsync"
        }else{//点赞
            str_url = btn.isSelected ? "api/services/app/Attitude/DeleteAttitudeRecordAsync" : "api/services/app/Attitude/CreateAttitudeRecordAsync"
        }

        if btn.isSelected {
            SDataCenter.shared.SDeleteDataWithUrl(str_url: str_url, params: params) {  (responseData, isSuccess) in
                if isSuccess{
                    btn.isSelected = false
                    if btn.tag == 201 {//收藏
                        ProgressHUD.showMsgWithoutView( "取消收藏成功")
                    }else{//点赞
                        ProgressHUD.showMsgWithoutView( "取消点赞成功")
                    }
                    
                }
            }
        }else{
            SDataCenter.shared.SPostJsonDataWithUrl(str_url: str_url, params: params) { (responseData, isSuccess) in
                ProgressHUD.hide()
                if isSuccess{
                    btn.isSelected = true
                    if btn.tag == 201 {//收藏
                        ProgressHUD.showMsgWithoutView("收藏成功")
                    }else{//点赞
                        ProgressHUD.showMsgWithoutView("点赞成功" )
                    }
                }
            }
        }
        
      
    }
    
    func changeTheBtnImgAndTitle(button:UIButton) -> Void {
        let offset = 25.0
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: -button.imageView!.frame.size.width, bottom: -button.imageView!.frame.size.height - CGFloat(offset/2), right: 0)
        button.imageEdgeInsets = UIEdgeInsets(top: -(button.titleLabel?.intrinsicContentSize.height)! - CGFloat(offset/2), left: 0, bottom: 0, right: -(button.titleLabel?.intrinsicContentSize.width)!)
        
    }
    
    
    
}
extension BottomCommentView : UITextFieldDelegate{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        sendUserComment()
        return true
    }
    //MARK:-发布评论
    func sendUserComment() -> Void {
        if commentField.text?.count == 0 {
            ProgressHUD.showMsgWithoutView("请输入评论内容")
            return
        }
        var params = [String : Any]()
        params["moduleTypeId"] = 100301
        params["commentedObjectId"] = self.blogId
        params["body"] = commentField.text

        SDataCenter.shared.SPostJsonDataWithUrl(str_url: "api/services/app/Comment/CreateOrUpdateCommentAsync", params: params) { (responseData, isSuccess) in
            ProgressHUD.hide()
            if isSuccess{
                self.commentField.resignFirstResponder()
                ProgressHUD.showMsgWithoutView("评论成功")
                if self.refreshCommentInfo != nil{
                    self.refreshCommentInfo!(0)
                }
            }
        }
    }
}
